<template>
  <div>
    <header_slot></header_slot>
    <select_slot></select_slot>
    <div class="wrap">
      <div class="body">
        <!-- 头部logo和搜索 -->
        <div class="hand">
          <el-row :gutter="20">
            <el-col :span="6">
              <!-- <div class="imgs"></div> -->
            </el-col>
            <el-col :span="7" :offset="11">
              <div class="trs">
                <tr>
                  <td>
                    <el-input
                      placeholder="请输入内容"
                      v-model="input"
                      clearable
                    ></el-input>
                  </td>
                  <td>
                    <el-button type="primary" icon="el-icon-search"
                      >搜索</el-button
                    >
                  </td>
                </tr>
              </div>
            </el-col>
          </el-row>
        </div>
        <!-- 购物车列表 -->
        <el-table
          ref="multipleTable"
          :data="Arr"
          tooltip-effect="dark"
          style="width: 100%"
          v-model="checked"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55"></el-table-column>
          <!-- 图片 -->
          <el-table-column prop="goodsImage" label>
            <template slot-scope="scope">
              <!-- {{scope.row.goodsImage}} -->
              <img :src="scope.row.goodsImage" alt />
            </template>
          </el-table-column>
          <el-table-column
            prop="goodsName"
            label="商品"
            width="300"
          ></el-table-column>
          <el-table-column prop="goodsPrice" label="单价"></el-table-column>

          <el-table-column label="数量" align="center">
            <template slot-scope="scope">
              <el-input-number
                @change="handleChange(scope.$index, scope.row.number)"
                size="mini"
                v-model="scope.row.number"
                :min="1"
                :max="10000000"
                label="描述文字"
              ></el-input-number>
            </template>
          </el-table-column>

          <el-table-column label="小计" align="center" show-overflow-tooltip>
            <template slot-scope="scope">{{ scope.row.total }}元</template>
          </el-table-column>

          <el-table-column prop="address" label="操作" show-overflow-tooltip>
            <template slot-scope="scope">
              <el-button @click="dialogVisible = true" type="text" size="small"
                >移除</el-button
              >
              <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
                <span>是否确认删除？</span>
                <span slot="footer" class="dialog-footer">
                  <el-button @click="dialogVisible = false">取 消</el-button>
                  <el-button type="primary" @click="dat(scope.row)"
                    >确 定</el-button
                  >
                </span>
              </el-dialog>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-row type="flex" class="row-bg" justify="end">
        <el-col :span="1">
          <div class="grid-content bg-purple"></div>
        </el-col>
        <el-col :span="3">
          <div class="total_warpper">
            <p>
              已选择
              <b>{{ totalNumber }}</b> 件商品
            </p>
            <p>
              总价：
              <b>{{ totalPrice }}元</b>
            </p>
          </div>
        </el-col>
        <el-col :span="10">
          <el-button type="danger" @click="cli()">立即购买</el-button>
        </el-col>
      </el-row>
    </div>
    <footer_slot></footer_slot>
  </div>
</template>
<script>
import axios from "axios";
import header_slot from "./header-slot";
import select_slot from "./select-slot";
import footer_slot from "./footer-slot";
export default {
  components: {
    header_slot,
    select_slot,
    footer_slot,
  },
  data() {
    return {
      dialogVisible: false,
      multipleSelection: [], //存放已选择的数据
      input: "",
      checked: true,
      Arr: [],
      Id: "",
      number:""
    };
  },
  methods: {
    dat(e) {
      axios
        .post("shopping/del", {
          id: e.id,
        })
        .then((res) => {
        });
      this.dialogVisible = false;
      location.reload();
    },
    handleSelectionChange(val) {
      
      this.multipleSelection = val; //给定义的数组赋值
      if (val.length != 0) {
        this.Id = val[0].id;
      }
    },
    handleChange(idx, val) {
      this.Arr[idx].total = val * this.Arr[idx].goodsPrice;
    },
    cli() {
      if (this.totalNumber != 0) {
        this.$router.push({
          path: "/pay",
          query: { totalPrice: this.totalPrice, ids: this.Id, number: this.multipleSelection[0].number },
        });
      } else {
        alert("请选择商品");
      }
    },
  },
  // 通过computed计算属性及时改变
  computed: {
    // 总价
    totalPrice() {
      let price_total = 0;
      this.multipleSelection.forEach((item) => {
        price_total += Number(item.total);
      });
      return price_total;
    },
    // 总数
    totalNumber() {
      var number_total = 0;
      this.multipleSelection.map((item) => {
        return (number_total += item.number);
      });

      return number_total;
    },
    somePrice() {
      var price_some = 0;
      this.multipleSelection.forEach((item) => {
        return (price_some += item.price * item.number);
      });
      return price_some;
    },
  },
  mounted() {
    axios
      .post("http://localhost:9500/shopping/findAllByUserId", {
        userId: sessionStorage.getItem("userId"),
      })
      .then((res) => {
        let Arrs = [];
        let goodsList = res.data.data.goodsList;
        let shoppingLIst = res.data.data.shoppingLIst;
        for (let i = 0; i < goodsList.length; i++) {
          Arrs[i] = {};
          Arrs[i].total = shoppingLIst[i].number * goodsList[i].goodsPrice;
          Arrs[i].goodsName = goodsList[i].goodsName;
          Arrs[i].goodsPrice = goodsList[i].goodsPrice;
          Arrs[i].goodsImage = goodsList[i].goodsImage;
          Arrs[i].number = shoppingLIst[i].number;
          Arrs[i].id = shoppingLIst[i].id;
        }
        this.Arr = Arrs;
      });
  },
};
</script>
<style scoped>
img{
  height: 100px;
  width: 100px;
}
.body {
  width: 100%;
  /* background-color: rebeccapurple; */
}
.hand .imgs {
  width: 200px;
  height: 200px;
  background-color: black;
}
.trs {
  margin-top: 70px;
}
p {
  font-size: 15px;
  color: #000;
}
b {
  font-size: 15px;
  color: #e64347;
}
</style>